<?php
class ControllerDeployHd extends Controller {
        public function index(){
            if(isset($this->request->post['app_id'])){
                $this->data['app_id']=$this->request->post['app_id'];
            }else{
                $this->data['app_id']=0;
            }

            $this->data['region']=$this->request->post['region'];
            $this->data['hd_name']=$this->request->post['hd_name'];
            $this->data['size']=$this->request->post['size'];
             $this->data['type']=$this->request->post['type'];
            $this->data['encryp_type']=$this->request->post['encryp_type'];
             $this->data['pin']=$this->request->post['pin'];
           
            

            if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/deploy/hd.tpl')) {
                $this->template = $this->config->get('config_template') . '/template/deploy/hd.tpl';
            } else {
                $this->template = 'default/template/deploy/hd.tpl';
            }
            $this->response->setOutput($this->render());

        }
        public function check_hd_name_exist(){
        	 $hdname = $this->request->post['hdname'];
        	 $region = $this->request->post['region'];
            if(!$this->backend->check_hd_name_exist($region,$hdname, $this->customer->getEmail(), $this->customer->getPassPlain())){
                $json['result']="success";
            }else{
                $json['result']="exist";
            }
            $this->response->setOutput(json_encode($json));
        	
          
        }
        public function creat_hd(){
        	 $this->language->load('deploy/hd');

            $json = array();
            // Validate if customer is logged in.
            if (!$this->customer->isLogged()) {
                $json['redirect'] = $this->url->link('checkout/checkout', '', 'SSL');
            }
            // Validate cart has products and has stock.
            if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
                $json['redirect'] = $this->url->link('checkout/cart');
            }
            // Validate Harddisk Name
            if (!$json) {
                if (!preg_match('/^[0-9a-zA-Z_]{3,32}$/i', $this->request->post['hd_name'])) {
                    $json['error']['hd_name'] = $this->language->get('error_hd_name');
                }
				$reminder = $this->request->post['size'] % 10;
                if(is_numeric($this->request->post['size'])&&$this->request->post['size']>=10&&$this->request->post['size']<=500&&$reminder == 0){

                }else{
                    $json['error']['size'] = $this->language->get('error_size');
                }

//                if(isset($this->session->data['app']['hd'][$this->request->post['app_id']]['hd_name'])&&$this->request->post['hd_name']==$this->session->data['app']['hd'][$this->request->post['app_id']]['hd_name']){
//               		
//               		$json['error']['hd_name'] = $this->language->get('error_hd_name_exist');
//                }
                
                if($this->backend->check_hd_name_exist($this->request->post['region'],$this->request->post['hd_name'], $this->customer->getEmail(), $this->customer->getPassPlain())){
                    $json['error']['hd_name'] = $this->language->get('error_hd_name_exist');
                }
                if($this->request->post['encryp_type'] == 1){
                	if (!$this->check->check_str_len($this->request->post['pin'], 8, 32) || (!$this->check->check_up_low_number_exist($this->request->post['pin']))) {
                		$json['error']['pin'] = $this->language->get('error_password');
                	}
              
            	}
                
            }
            // Update session
            if (!$json) {
                $hd_name="hd_".substr(md5(uniqid(rand(), true)), 0, 9);
                $this->session->data['app']['hd'][$this->request->post['app_id']]=array(
                   
                    'region'  =>trim($this->request->post['region']),
                    'hd_name'  => trim($this->request->post['hd_name']),
                    'size'  => trim($this->request->post['size']),
                    'type'  => trim($this->request->post['type']),
                    'encryp_type'  => trim($this->request->post['encryp_type']),
                    'pin'  => trim($this->request->post['pin']),
                    
                );
                $json="succsess";
            }
            
            $this->response->setOutput(json_encode($json));
        	
           
    	}
        public function edit_hd(){
        	 $this->language->load('deploy/hd');

            $json = array();
            // Validate if customer is logged in.
            if (!$this->customer->isLogged()) {
                $json['redirect'] = $this->url->link('checkout/checkout', '', 'SSL');
            }
            // Validate cart has products and has stock.
            if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
                $json['redirect'] = $this->url->link('checkout/cart');
            }
            // Validate Harddisk Name
            if (!$json) {
                if (!preg_match('/^[0-9a-zA-Z_]{3,32}$/i', $this->request->post['hd_name'])) {
                    $json['error']['hd_name'] = $this->language->get('error_hd_name');
                }
				$reminder = $this->request->post['size'] % 10;
                if(is_numeric($this->request->post['size'])&&$this->request->post['size']>=10&&$this->request->post['size']<=500&&$reminder == 0){

                }else{
                    $json['error']['size'] = $this->language->get('error_size');
                }
				
//                if(isset($this->session->data['app']['hd'][$this->request->post['app_id']]['hd_name'])&&$this->request->post['hd_name']==$this->session->data['app']['hd'][$this->request->post['app_id']]['hd_name']){
//               		
//               		$json['error']['hd_name'] = $this->language->get('error_hd_name_exist');
//                }
                
                if($this->backend->check_hd_name_exist($this->request->post['app_id'],$this->request->post['hd_name'], $this->customer->getEmail(), $this->customer->getPassPlain())){
                    $json['error']['hd_name'] = $this->language->get('error_hd_name_exist');
                }
            }
            // Update session
            if (!$json) {
                $hd_name="hd_".substr(md5(uniqid(rand(), true)), 0, 9);
                $this->session->data['app']['hd'][$this->request->post['app_id']]=array(
                   
                    'region'  =>trim($this->request->post['region']),
                    'hd_name'  => trim($this->request->post['hd_name']),
                    'size'  => trim($this->request->post['size']),
                    'type'  => trim($this->request->post['type']),
                    'encryp_type'  => trim($this->request->post['encryp_type']),
                    'pin'  => trim($this->request->post['pin']),
                    
                );
                $json="succsess";
            }
            $this->response->setOutput(json_encode($json));
        	
       
    	}

        public function edit(){
        	 if(isset($this->request->post['app_id'])){
               $app_id=$this->request->post['app_id'];
            }else{
               $app_id=0;
            }

            $this->data['app_id'] =$app_id;
            $this->data['edit'] =true;;
            $this->data['region']=$this->session->data['app']['hd'][$app_id]['region'];
            $this->data['hd_name']=$this->session->data['app']['hd'][$app_id]['hd_name'];
            $this->data['size']=$this->session->data['app']['hd'][$app_id]['size'];
            $this->data['type']=$this->session->data['app']['hd'][$app_id]['type'];
            
            $this->data['encryp_type']=$this->session->data['app']['hd'][$app_id]['encryp_type'];
            $this->data['pin']=$this->session->data['app']['hd'][$app_id]['pin'];
            
            if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/deploy/hd.tpl')) {
                $this->template = $this->config->get('config_template') . '/template/deploy/hd.tpl';
            } else {
                $this->template = 'default/template/deploy/hd.tpl';
            }
            $this->response->setOutput($this->render());

        }
           

    	}


?>
